package jpa.entities;

import java.io.Serializable;
import java.util.GregorianCalendar;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.apache.tomcat.util.http.fileupload.FileItem;

/**
 * @author Remco Haszing
 */
@Entity
@Table(name = "Revision")
public class Revision implements Serializable {

    // properties
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private int version;
    private String filename;
    private String contentType;
    @Temporal(TemporalType.DATE)
    private GregorianCalendar uploadDate;
    @OneToOne
    private Employee uploader;
    @ManyToOne(cascade = CascadeType.PERSIST)
    private Item item;

    // constructors
    /**
     * This constructor is used by the JPA API and should not be used for
     * anything else.
     *
     * @deprecated Use {@link #Revision(FileItem)} instead.
     */
    @Deprecated
    public Revision() {
    }

    /**
     * Creates a new revision for an item. This method creates a new revision
     * for an item.
     *
     * @param version The version of this revision.
     * @param file The file item of the new revision.
     * @param uploader The employee who uploaded this revision.
     */
    public Revision(int version, FileItem file, Employee uploader, Item item) {
        this.version = version;
        this.filename = file.getName();
        this.contentType = file.getContentType();
        this.uploader = uploader;
        this.uploadDate = new GregorianCalendar();
        this.item = item;
    }

    // getters
    /**
     * @return The unique id number of this revision.
     */
    public long getId() {
        return this.id;
    }

    /**
     * @return The version of this revision.
     */
    public int getVersion() {
        return this.version;
    }

    /**
     * @return The filename of the stored file of this revision.
     */
    public String getFilename() {
        return this.filename;
    }

    /**
     * @return The content type of the stored file of this revision.
     */
    public String getContentType() {
        return this.contentType;
    }

    /**
     * @return The person who uploaded this revision.
     */
    public Employee getUploader() {
        return this.uploader;
    }

    /**
     * @return The date this revision was created.
     */
    public GregorianCalendar getUploadDate() {
        return this.uploadDate;
    }

    public Item getItem() {
        return this.item;
    }
}
